﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pifagor.IG;
using Pifagor.Exceptions;

namespace Pifagor.Runtime
{
	public class ExternalFunction : IFunction
	{
		ExecutionContext _context;

		internal ExternalFunction(ExecutionContext context)
		{
			_context = context;
			if (_context.IG.RootNodes.Length != 1)
				throw new RuntimeException("There must be only one root node for function");
		}

		public Value Run(Value argument)
		{
			var rootNode = _context.IG.Nodes[_context.IG.RootNodes[0].Id];
			return ComputableFactory.CreateComputable(_context.SetFunctionArgument(argument), rootNode).Compute();
		}
	}
}
